package com.nny.alarm.view;

import com.uxwine.util.UxToast;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class EditLayout extends LinearLayout {
	
	boolean mbLock;
	EditHolder mEditHolder;
	GestureDetector mGesture;
	int mnTouchSlop;
	boolean mbTouchIntercept;
	public static final int FLING_UP 	= 0;
	public static final int FLING_DOWN 	= 1;
	public static final int FLING_LEFT 	= 2;
	public static final int FLING_RIGHT = 3;

	public EditLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}
	
	public void lock(boolean bLock)
	{
		mbLock = bLock;
	}
	
	public void setEditHolder(EditHolder h)
	{
		mEditHolder = h;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		if(mbLock)
		{
			if(action == MotionEvent.ACTION_DOWN && mEditHolder != null)
			{
				mEditHolder.onLockedClick(this);
			}
			return true;
		}
		if(action == MotionEvent.ACTION_DOWN)
		{
			if(super.dispatchTouchEvent(ev))
			{
				mbTouchIntercept = true;
			}
			else
			{
				mbTouchIntercept = false;
				mGesture.onTouchEvent(ev);				
			}
			return true; //gesture need return true
		}
		else
		{
			if(mbTouchIntercept)
			{

				if(action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP)
				{
					mbTouchIntercept = false;
				}
				return super.dispatchTouchEvent(ev);
			}
			else
			{
				mbTouchIntercept = false;
				return mGesture.onTouchEvent(ev);
			}
		}
	}	

	private void initView()
	{
		final Context context = getContext();
		mGesture = new GestureDetector(new MOnGDlistener());
		mnTouchSlop = ViewConfiguration.get(context).getScaledDoubleTapSlop();
		mbLock = false;
 	}
	
	private void onFlingEvent(int type)
	{
		if(mbLock)
			return;
		if(null != mEditHolder)
			mEditHolder.onEditFling(this, type);
	}
	
	public static interface EditHolder
	{
		public void onLockedClick(EditLayout v);
		public void onEditFling(EditLayout v, int type);
	}
	
	private class MOnGDlistener extends SimpleOnGestureListener
	{
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(e1 == null || e2 == null)
				return false;
			float dx = e1.getX() - e2.getX();
			float dy = e1.getY() - e2.getY();
			float adx = Math.abs(dx);
			float ady = Math.abs(dy);
			if(adx > mnTouchSlop)
			{
				float avx = Math.abs(velocityX);
				if(avx > 1000)
				{
					if(dx > 0)
						onFlingEvent(FLING_LEFT);
					else
						onFlingEvent(FLING_RIGHT);
					return true;
				}
			}
			else if(ady > mnTouchSlop)
			{
				float avy = Math.abs(velocityY);
				if(avy > 1000)
				{
					if(dy < 0)
						onFlingEvent(FLING_DOWN);
					else
						onFlingEvent(FLING_UP);
					return true;
				}
			}
			return false;
		}		
	}
}
